package com.psa.element;

import android.content.ContentValues;
import android.database.Cursor;
import com.google.gson.Gson;
import com.psa.tool.DBTool;

/**
 * @author david
 */
public class Showcase implements IJson,ISqlObject {
    public int PK_ID = 0;
    public String NAME = "";
    public String DETAIL = "";
    public String STARTDATE = "";
    public String ENDDATE = "";
    public int ORDERSEQ = 0;
    public int DR = 0;
    public int csc_ID = -1;
    public String CustomerID = "";
    public String qyDate = "";
    public String jsDate = "";

    @Override
    public String toJson() {
        return new Gson().toJson(this);
    }

    @Override
    public int insert(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("PK_ID", PK_ID);
        values.put("NAME", NAME.trim());
        values.put("DETAIL", DETAIL.trim());
        values.put("STARTDATE", STARTDATE.trim());
        values.put("ENDDATE", ENDDATE.trim());
        values.put("ORDERSEQ", ORDERSEQ);
        values.put("DR", DR);
        return (int)dbTool.insert("Showcase", values);
    }

    @Override
    public int update(DBTool dbTool) {
        ContentValues values = new ContentValues();
        values.put("NAME", NAME.trim());
        values.put("DETAIL", DETAIL.trim());
        values.put("STARTDATE", STARTDATE.trim());
        values.put("ENDDATE", ENDDATE.trim());
        values.put("ORDERSEQ", ORDERSEQ);
        values.put("DR", DR);
        String where = "PK_ID = ?";
        String[] whereval = { PK_ID+"" };
        return dbTool.update("Showcase", values, where, whereval);
    }

    @Override
    public int delete(DBTool dbTool) {
        String where = "PK_ID = ?";
        String[] wherevalue = { PK_ID+"" };
        return dbTool.delete("Showcase", where, wherevalue);
    }

    @Override
    public ISqlObject full(Cursor cursor) {
        if (cursor!=null && cursor.moveToNext()) {
            Showcase showcase = new Showcase();
            showcase.PK_ID = cursor.getInt(0);
            showcase.NAME = cursor.getString(1);
            showcase.DETAIL = cursor.getString(2);
            showcase.STARTDATE = cursor.getString(3);
            showcase.ENDDATE = cursor.getString(4);
            showcase.ORDERSEQ = cursor.getInt(5);
            showcase.DR = cursor.getInt(6);
            return showcase;
        }
        return null;
    }

    @Override
    public void clear(DBTool dbTool) {
        dbTool.delete("Showcase", "PK_ID Like ?", new String[]{"%"});
    }

    @Override
    public String getDataSql() {
        return "Showcase";
    }

    @Override
    public String getSyncMethod() {
        return "getdataobjects";
    }

    @Override
    public String toString() {
        return PK_ID + " " + NAME ;
    }

    public static Showcase getShowcase(int pk_id) {
        String sql = "Select * From Showcase Where pk_id=" + pk_id;
        Cursor cursor = DBTool.getInstance().query(sql);
        ISqlObject showcase = new Showcase();
        showcase = showcase.full(cursor);
        return showcase != null? (Showcase)showcase : null;
    }
}
